package com.ruoyi.workflow.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.workflow.mapper.DefinitionNodeMapper;
import com.ruoyi.workflow.domain.DefinitionNode;
import com.ruoyi.workflow.service.IDefinitionNodeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 流程定义节点信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-17
 */
@Service
public class DefinitionNodeServiceImpl extends ServiceImpl<DefinitionNodeMapper, DefinitionNode> implements IDefinitionNodeService {

    /**
     * 查询流程定义节点信息列表
     *
     * @param definitionNode 流程定义节点信息
     * @return 流程定义节点信息
     */
    @Override
    public List<DefinitionNode> selectDefinitionNodeList(DefinitionNode definitionNode) {
        return getBaseMapper().selectDefinitionNodeList(definitionNode);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(DefinitionNode definitionNode) {
        boolean save = super.save(definitionNode);
        if(!CollectionUtils.isEmpty(definitionNode.getHead())){
            getBaseMapper().insertRelation(definitionNode.getId(),definitionNode.getHead());
        }
        return save;
    }

    @Override
    public List<SysUser> selectUserByNodeKey(String nodekey) {
        return getBaseMapper().selectUserByNodeKey(nodekey);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateById(DefinitionNode entity) {
        boolean update = super.updateById(entity);
        if(!CollectionUtils.isEmpty(entity.getHead())){
            getBaseMapper().deleteRelation(entity.getId());
            getBaseMapper().insertRelation(entity.getId(),entity.getHead());
        }
        return update;
    }
   public boolean removeBatchByIds(Collection<?> ids){
       boolean batch = super.removeBatchByIds(ids);
       for (Object id : ids) {
           getBaseMapper().deleteRelation((Long) id);
       }
       return batch;
   }

    @Override
    public DefinitionNode getById(Serializable id) {
        DefinitionNode bpmnNode = super.getById(id);
       List<Long> head = getBaseMapper().selectHeadByNodeId(id);
       bpmnNode.setHead(head);
                return bpmnNode;
    }
}
